﻿using NHModel.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NHDemo
{
    public partial class Index : System.Web.UI.Page
    {
        private delegate UserInfo GetMethod(int Id);
        private delegate int AddMethod(UserInfo user);
        private delegate void UpdateMethod(UserInfo user);
        private delegate void DeleteMethod(int Id);
        private delegate string GetNameMethod(int Id);

        private GetMethod Get;
        private AddMethod Add;
        private UpdateMethod Update;
        private DeleteMethod Delete;
        private GetNameMethod GetName;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetData();
            }
        }

        #region 基本操作
        /// <summary>
        /// Get
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnGet_Click(object sender, EventArgs e)
        {
            int Id=Convert.ToInt32(txtGetId.Text.Trim());
            string mode = rbGetMode.SelectedValue;
            SelectFunction(mode);
            UserInfo user=Get(Id);
            lblGetDisplay.Text = Display(user);
        }

        /// <summary>
        /// Add
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            UserInfo user = new UserInfo();
            user.Name = txtAddName.Text;
            user.Sex = txtAddSex.Text;
            user.Age = Convert.ToInt32(txtAddAge.Text);
            SelectFunction("");
            Add(user);
            GetData();
            
        }
        /// <summary>
        /// UpdateGet
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdateGet_Click(object sender, EventArgs e)
        {
            int Id = Convert.ToInt32(txtUpdateId.Text.Trim());
            SelectFunction("");
            UserInfo user = Get(Id);
            txtUpdateName.Text = user.Name;
            txtUpdateSex.Text = user.Sex;
            txtUpdateAge.Text = user.Age.ToString();
        }
        /// <summary>
        /// Update
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
           
            UserInfo user = new UserInfo();
            user.Id = Convert.ToInt32(txtUpdateId.Text);
            user.Name = txtUpdateName.Text;
            user.Sex = txtUpdateSex.Text;
            user.Age = Convert.ToInt32(txtUpdateAge.Text);
            SelectFunction("");
            Update(user);
            GetData();
        }
        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int Id = Convert.ToInt32(txtDeleteId.Text);
            SelectFunction("");
            Delete(Id);
            GetData();
        }

        /// <summary>
        /// GetName
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnGetName_Click(object sender, EventArgs e)
        {
            int Id = Convert.ToInt32(txtGetNameId.Text.Trim());
            string mode = rbGetNameMode.SelectedValue;
            SelectFunction(mode);
            lblGetNameDisplay.Text = GetName(Id);
        }
        #endregion


        #region 辅助页面呈现
        /// <summary>
        /// 获取全部数据
        /// </summary>
        public void GetData()
        {
            UserInfoBll bll = new UserInfoBll();
            gv.DataSource = bll.SearchAll_HQL();
            gv.DataBind();
        }
        /// <summary>
        /// 呈现一条数据
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public string Display(UserInfo user)
        {
           return string.Format("Id:{0},Name:{1},Sex:{2},Age:{3}", user.Id, user.Name, user.Sex, user.Age);
        }

        public void SelectFunction(string mode)
        {
            UserInfoBll bll=new UserInfoBll();
         
            switch (mode)
            {
               case "0":
                    Get=bll.Get;
                    break;
                case "1":
                    Get = bll.Get_HQL;
                    GetName = bll.GetName_HQL;
                    break;
                case "2":
                    Get = bll.Get_SQL;
                    GetName = bll.GetName_SQL;
                    break;
                default:
                    Get = bll.Get;
                    GetName = bll.GetName_HQL;
                    break;
            }
            Add = bll.Add;
            Update = bll.Update;
            Delete = bll.Delete;
        }
        #endregion
    }
}